﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InformationSystem.BLL.Core.Model;
using InformationSystem.BLL.Core.Service;
using System.IO;

namespace InformationSystem.WebSite.Module.NoticeMgr
{
    public partial class InformationAttachmentDownLoad : BasePage
    {
        protected LoginUser user;
        public IInformationService InformationService { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            user = (LoginUser)Session["LoginUser"];
            //mailid
            if (Request.QueryString["type"] == null || Request.QueryString["objectid"] == null || Request.QueryString["fileid"] == null)
                return;
            string type = "";
            int objectid = 0;
            int fileid = 0;
            type = Request.QueryString["type"];
            if (!int.TryParse(Request.QueryString["objectid"], out objectid))
                return;
            if (!int.TryParse(Request.QueryString["fileid"], out fileid))
                return;
            string path = string.Empty;
            Attachment att = null;
            switch (type)
            {
                case "affiche":
                    att = InformationService.GetAfficheAttachment(objectid, fileid);
                    if (att == null) return;
                    path = Path.Combine(Server.MapPath("~"), Common.AfficheAttachmentPath, att.SavedFileName);
                    break;
                case "msg":
                    att = InformationService.GetMsgAttachment(objectid, fileid);
                    if (att == null) return;
                    path = Path.Combine(Server.MapPath("~"), Common.MsgAttachmentPath, att.SavedFileName);
                    break;
                case "workinfo":
                    att = InformationService.GetWorkInfoAttachment(objectid, fileid);
                    if (att == null) return;
                    path = Path.Combine(Server.MapPath("~"), Common.WorkAttachmentPath, att.SavedFileName);
                    break;
            }

            if (path == string.Empty) return;
            if (!File.Exists(path)) return;

            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

            String httpHeader = "attachment;filename=" + HttpUtility.UrlEncode(att.UploadFileName);
            this.Response.Clear();
            this.Response.AddHeader("Content-Disposition", httpHeader);
            this.Response.WriteFile(path);
            this.Response.End();
        }

        protected override bool CommitPermissionCheck()
        {
            throw new NotImplementedException();
        }
    }
}